﻿using DatabaseModel;
using LogBookImporter.Exceptions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LogBookImporter.Extensions;

namespace LogBookImporter.Implementation {
    public class LogBookEntryCreator {

        private Dictionary<string, int> _dico = new Dictionary<string, int>();

        public List<LogBookEntry> CreateLogBookEntries(List<string> logBookContent, Logger.Logger logger, string fileLocation) {
            List<LogBookEntry> result = new List<LogBookEntry>();
            int lineNumber = 0;
            foreach(string logBookLine in logBookContent) {
                try {
                    lineNumber++;
                    LogBookEntry logBookEntry = new LogBookEntry();
                    string[] splitedMainLine = LogBookLineParser.ParseMainLine(logBookLine);
                    string fileLocationWithDelta = splitedMainLine[0];
                    string subject = splitedMainLine[1];
                    string temperature = splitedMainLine[3];
                    string angles = splitedMainLine[4];
                    string scans = splitedMainLine[5];
                    string resulotion = splitedMainLine[6];
                    string macro = splitedMainLine[7];
                    string time = splitedMainLine[8];

                    logBookEntry.filename = LogBookLineParser.ParseFileLocation(fileLocationWithDelta);
                    int delta = LogBookLineParser.ParseDelta(splitedMainLine[0]);

                    // fixes a possible inconsistence of filenames and delta numbers which is caused by the software creating the logbook.txt files
                    logBookEntry.filename = AddFilenameToDico(logBookEntry.filename, delta);


                    logBookEntry.aoi = LogBookLineParser.ParseAngles(angles)[0];
                    logBookEntry.pola = LogBookLineParser.ParseAngles(angles)[1];


                    logBookEntry.date = LogBookLineParser.ParseDateTimeFromFileLocation(time, logBookEntry.filename);


                    logBookEntry.macro = macro;
                    logBookEntry.resolution = LogBookLineParser.ParseResolution(resulotion);
                    logBookEntry.scans = LogBookLineParser.ParseScans(scans);
                    logBookEntry.subject = subject;
                    logBookEntry.temperature = double.Parse(temperature);
                    result.Add(logBookEntry);
                    
                }
                catch (Exception e) {
                    ImporterException customException = new ImporterException();
                    customException.LineNumber = lineNumber;
                    customException.CustomMessage = e.GetaAllMessages();
                    logger.Log(string.Format("Error creating logbook entry object in memory for file '{0}'.\n Exception: {1}", fileLocation, customException.GetaAllMessages()));
                }
                
            }
            return result;
        }


        private string AddFilenameToDico(string fileName, int delta) {
            if (!_dico.ContainsKey(fileName)) {
                _dico[fileName] = delta;
                return fileName;
            }
            else {
                string newFileName = LogBookLineParser.CreateFileLocationByDelta(fileName, _dico[fileName]);
                _dico[newFileName] = 1;
                _dico[fileName]++;
                return newFileName;
            }
        }
    }
}
